package com.xqq.utils;

import cn.hutool.core.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.Date;

import static com.xqq.utils.Constants.DATE_PATTERN;

@Slf4j
public class CommonUtil {
    private static final double EARTH_RADIUS = 6371;
    public static boolean isValidLatitude(double latitude) {
        return latitude >= -90 && latitude <= 90;
    }
    public static boolean isValidLongitude(double longitude) {
        return longitude >= -180 && longitude <= 180;
    }
    public static double calculateDistance(double sourceLon, double sourceLat, double targetLon, double targetLat) {
        if (!isValidLatitude(sourceLat)) {
            return -1;
        }
        if (!isValidLongitude(sourceLon)) {
            return -1;
        }
        if (!isValidLatitude(targetLat)) {
            return -1;
        }
        if (!isValidLongitude(targetLon)) {
            return -1;
        }
        // 将经纬度转换为弧度
        double radLat1 = Math.toRadians(sourceLat);
        double radLon1 = Math.toRadians(sourceLon);
        double radLat2 = Math.toRadians(targetLat);
        double radLon2 = Math.toRadians(targetLon);

        // 应用 Haversine 公式计算距离
        double dlon = radLon2 - radLon1;
        double dlat = radLat2 - radLat1;
        double a = Math.sin(dlat / 2) * Math.sin(dlat / 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.sin(dlon / 2) * Math.sin(dlon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = EARTH_RADIUS * c;
        return NumberUtil.round(distance, 2).doubleValue() * 1000;
    }
    public static long convertTUCtodatetimeEpoch(String utcTime){
        try {
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(utcTime);
            OffsetDateTime beijingTime = offsetDateTime.withOffsetSameInstant(ZoneOffset.ofHours(8));
            OffsetDateTime roundedTime = beijingTime.withMinute(0).withSecond(0).withNano(0);
            return roundedTime.toEpochSecond();
        } catch (DateTimeParseException e) {
            log.error("Invalid UTC time format: " + utcTime);
            return 0;
        }
    }
    public static String getTodayTime() {
        return new SimpleDateFormat(DATE_PATTERN).format(new Date());
    }

    public static void main(String[] args) {

        String source = "98.58765,24.43708";
        String target = "98.58769,24.43746";

        double sourceLon = 98.599945;
        double sourceLat = 24.477841;
        double targetLon = 98.60227;
        double targetLat = 24.47989;
        sourceLon = Double.parseDouble(source.split(",")[0]);
        sourceLat = Double.parseDouble(source.split(",")[1]);
        targetLon = Double.parseDouble(target.split(",")[0]);
        targetLat = Double.parseDouble(target.split(",")[1]);
        double v = calculateDistance(sourceLon, sourceLat, targetLon, targetLat);
        System.out.println(v);
    }
}
